package com.chookss.view;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.chookss.R;
import com.chookss.view.listener.WeekDialogListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


public class WeekDialog extends BaseDialog {


    @BindView(R.id.minute_pv)
    PickerView minute_pv;
    @BindView(R.id.tvCancel)
    TextView tvCancel;
    @BindView(R.id.second_pv)
    PickerView second_pv;
    @BindView(R.id.tvSure)
    TextView tvSure;
    private Context context;
    private WeekDialogListener listener = null;
    private List<List<String>> allList = new ArrayList<>();
    private List<String> yearList = new ArrayList<>();
    private List<String> oldYearList = new ArrayList<>();
    private int yearIndex = 4, weekIndex = 42, size = 52;
    private String yeaChoice, weekChoice;

    public void WeekDialogListener(@NonNull Context context) {
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_week;
    }

    @Override
    protected void init(View view) {
        initCenter(0.3f);
        tvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(yeaChoice, weekChoice);
                dismiss();
            }
        });
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }


    public WeekDialog(@NonNull Context context, int year, int week, List<String> yearList, List<List<String>> allList, WeekDialogListener listener) {
        super(context);
        this.context = context;
        this.listener = listener;
        this.yearIndex = year;
        this.weekIndex = week;
        this.yearList = yearList;
        this.allList = allList;
        initdata();
    }

    private void initdata() {
        yearList.addAll(yearList);
        oldYearList.addAll(yearList);
        size = allList.get(yearIndex).size();
        for (int i = 0; i < allList.size(); i++) {
            allList.get(i).addAll(allList.get(i));
        }
        minute_pv.setData(yearList);
        second_pv.setData(allList.get(yearIndex));
        minute_pv.setSelected(yearIndex);
        if (weekIndex == 0) {
            second_pv.setSelected(size);
        } else {
            second_pv.setSelected(weekIndex);
        }
        yeaChoice = yearList.get(yearIndex);
        weekChoice = allList.get(yearIndex).get(weekIndex);
        minute_pv.setOnSelectListener(new PickerView.onSelectListener() {
            @Override
            public void onSelect(String data) {
                for (int i = 0; i < 6; i++) {
                    if (oldYearList.get(i).equals(data)) {
                        second_pv.setData(allList.get(i));
                        weekChoice = allList.get(i).get(allList.get(i).size() / 2);
                    }
                }
                yeaChoice = data;
            }
        });
        second_pv.setOnSelectListener(new PickerView.onSelectListener() {
            @Override
            public void onSelect(String data) {
                weekChoice = data;
            }
        });
    }


}